package com.tonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tonghe.domain.entity.Menu;
import com.tonghe.mapper.MenuMapper;
import com.tonghe.service.MenuService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单实现
 * @author tonghe
 * @since 2022-09-03 15:02:15
 */
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    /**
     * 查询用户权限
     *
     * @param id 用户id
     * @return 可用权限列表
     */
    @Override
    public List<String> selectPermsByUserId(Long id) {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        if(id == 1016L) {
            queryWrapper.in(Menu::getMenuType, "C","F");
            queryWrapper.eq(Menu::getStatus,0);
            List<Menu> menuList = list(queryWrapper);
            List<String> perms = menuList.stream().map(Menu::getPerms).collect(Collectors.toList());
            return perms;
        }
        return baseMapper.selectPermsByUserId(id);
    }

    /**
     * 查询用户可用菜单/路由
     *
     * @param userId 用户id
     * @return 可用‘路由/菜单’列表
     */
    @Override
    public List<Menu> selectRouterMenuTreeByUserId(Long userId) {

        List<Menu> menus = null;
        if(userId.equals(1016L)) {
            menus = baseMapper.selectAllRouterMenu();
        } else {
            menus = baseMapper.selectRouterMenuTreeByUserId(userId);
        }
        List<Menu> menuTree = buildMenuTree(menus, 0L);
        return menuTree;
    }

    @Override
    public List<Menu> selectMenuList(Menu menu) {

        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        //menuName模糊查询
        queryWrapper.like(StringUtils.hasText(menu.getMenuName()),Menu::getMenuName,menu.getMenuName());
        queryWrapper.eq(StringUtils.hasText(menu.getStatus()),Menu::getStatus,menu.getStatus());
        //排序 parent_id和order_num
        queryWrapper.orderByAsc(Menu::getParentId,Menu::getOrderNum);
        List<Menu> menus = list(queryWrapper);;
        return menus;
    }

    @Override
    public List<Long> selectMenuListByRoleId(Long roleId) {
        return getBaseMapper().selectMenuListByRoleId(roleId);
    }

    @Override
    public boolean hasChild(Long menuId) {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getParentId,menuId);
        return count(queryWrapper) != 0;
    }

    /** 构建菜单树形结构 */
    private List<Menu> buildMenuTree(List<Menu> menus, long parentId) {

        List<Menu> menuTree = menus.stream()
                .filter(menu -> menu.getParentId().equals(parentId))
                .map(menu -> menu.setChildren(getChildren(menu, menus)))
                .collect(Collectors.toList());
        return menuTree;
    }
    /** 获取该菜单的子菜单 */
    private List<Menu> getChildren(Menu menu, List<Menu> menus) {

        List<Menu> childrenList = menus.stream()
                .filter(m -> m.getParentId().equals(menu.getId()))
                .map(m -> m.setChildren(getChildren(m,menus)))
                .collect(Collectors.toList());
        return childrenList;
    }
}
